package readWirteExcel;

import jxl.*;
import model.ApiConfigDataModel;
import model.ApiDataImpl;
import model.ApiDataModel;
import model.TestResultAnalysisModel;
import tool.DataReplace;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by yangxianming on 2015-06-05.
 */
public class ReadExcelApi implements ApiDataImpl {
    final String dataFilePath = System.getProperty("user.dir") + "/data/";
    String dataFileFullPath = null;
    ArrayList<String> apiIds = new ArrayList<>();
    String thisApiId;
    ArrayList<ApiDataModel> apiList = new ArrayList();
    Workbook book;
    Sheet sheetConfig;
    Sheet sheetApi;
    Sheet sheetHeader;
    Sheet sheetParameters;
    Sheet sheetCatchdata;
    Sheet sheetResultAnalysis;

    ReadExcelApi(){
    }

//    初始化对象,列出需要测试api的关联值apiId,以提供只有需要测试的索引值
    ReadExcelApi(String projectDir, String fileName){
        dataFileFullPath = dataFilePath + projectDir + "/" + fileName;
        try {
            book = Workbook.getWorkbook(new File(dataFileFullPath));
            sheetApi = book.getSheet("api");
            int row = sheetApi.getRows();
            for (int y = 1; y < row; y++){
                if (sheetApi.getCell(0, y).getContents().equals("0"))
                    continue;
                apiIds.add(sheetApi.getCell(1, y).getContents());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        sheetConfig = book.getSheet("config");
        sheetHeader = book.getSheet("header");
        sheetParameters = book.getSheet("parameters");
        sheetCatchdata = book.getSheet("catchdata");
        sheetResultAnalysis = book.getSheet("resultAnalysis");
    }

    public ApiConfigDataModel getConfigData(ReadExcelApi readExcelApi){
        ApiConfigDataModel acdm = new ApiConfigDataModel();
        readExcelApi.readConfig(acdm);
        return acdm;
    }

    public ArrayList<ApiDataModel> getApiList(ReadExcelApi readExcelApi, ApiConfigDataModel acdm){
//        读取excel的配置文件,作为本次测试的全局配置
//        ApiConfigDataModel acdm = new ApiConfigDataModel();
//        readExcelApi.readConfig(acdm);

//        debug code
//        for (Map.Entry me:apiConfigDataModel.config.entrySet()){
//            System.out.printf("key:%s, value:%s%n", me.getKey(), me.getValue());
//        }

//        开始循环被测api
        for (String apiId: apiIds){
            thisApiId = apiId;
            ApiDataModel adm = new ApiDataModel();
            readExcelApi.readApiBase(adm, acdm);
            readExcelApi.readHeader(adm, acdm);
            readExcelApi.readParameters(adm, acdm);
            readExcelApi.readCatchData(adm, acdm);
            readExcelApi.readResultAnalysis(adm, acdm);
            apiList.add(adm);
        }
//        book.close();
        return apiList;
    }

    @Override
    public void readConfig(ApiConfigDataModel acdm) {
        //getCell(x,y)   第x列col , 第y行row
//        Sheet sheetConfig = book.getSheet("config");
        int row = sheetConfig.getRows();
        for (int y = 1; y < row; y++){
            if (sheetConfig.getCell(0, y).getContents().equals("0"))
                continue;
            acdm.config.put(
                    sheetConfig.getCell(1, y).getContents(),
                    sheetConfig.getCell(2, y).getContents());
        }
    }

    @Override
    public void readApiBase(ApiDataModel adm, ApiConfigDataModel acdm) {
//        getCell(x,y)   第x列col , 第y行row
        System.out.println("readApiBase apiId: "+thisApiId);
//        Sheet sheetApi = book.getSheet("api");
        int row = sheetApi.getRows();
        for (int y = 1; y < row; y++){
            if (sheetApi.getCell(0, y).getContents().equals("0"))
                continue;
            if (sheetApi.getCell(1, y).getContents().equals(thisApiId)){
                adm.apiId = sheetApi.getCell(1, y).getContents();
                adm.isExceptionStop = Boolean.valueOf(DataReplace.replace(acdm, sheetApi.getCell(2, y).getContents()));
                adm.method = DataReplace.replace(acdm, sheetApi.getCell(3, y).getContents());
                adm.domain = DataReplace.replace(acdm, sheetApi.getCell(4, y).getContents());
                adm.url = DataReplace.replace(acdm, sheetApi.getCell(5, y).getContents());
                adm.describe = DataReplace.replace(acdm, sheetApi.getCell(6, y).getContents());
            }
        }
    }



    @Override
    public void readHeader(ApiDataModel adm, ApiConfigDataModel acdm) {
//        getCell(x,y)   第x列col , 第y行row
//        System.out.println("--debug--readApiBase--apiId: "+thisApiId);
//        Sheet sheetHeader = book.getSheet("header");
        int row = sheetHeader.getRows();
        for (int y = 1; y < row; y++){
            if (sheetHeader.getCell(0, y).getContents().equals("0"))
                continue;
            String checkApiId = sheetHeader.getCell(1, y).getContents();
            if (checkApiId.equals(thisApiId) || checkApiId.equals("all")){
                adm.header.put(
                        DataReplace.replace(acdm, sheetHeader.getCell(2, y).getContents()),
                        DataReplace.replace(acdm, sheetHeader.getCell(3, y).getContents()));
            }
        }
    }

    @Override
    public void readParameters(ApiDataModel adm, ApiConfigDataModel acdm) {
//        getCell(x,y)   第x列col , 第y行row
//        System.out.println("--debug--readApiBase--apiId: "+thisApiId);
//        Sheet sheetParameters = book.getSheet("parameters");
        int row = sheetParameters.getRows();
        for (int y = 1; y < row; y++){
            if (sheetParameters.getCell(0, y).getContents().equals("0"))
                continue;
            if (sheetParameters.getCell(1, y).getContents().equals(thisApiId)){
                adm.parameters.put(
                        DataReplace.replace(acdm, sheetParameters.getCell(2, y).getContents()),
                        DataReplace.replace(acdm, sheetParameters.getCell(3, y).getContents())
                );
            }
        }
    }

    @Override
    public void readCatchData(ApiDataModel adm, ApiConfigDataModel acdm) {
        //        getCell(x,y)   第x列col , 第y行row
//        System.out.println("--debug--readApiBase--apiId: "+thisApiId);
//        Sheet sheetCatchdata = book.getSheet("catchdata");
        int row = sheetCatchdata.getRows();
        for (int y = 1; y < row; y++){
            if (sheetCatchdata.getCell(0, y).getContents().equals("0"))
                continue;
            if (sheetCatchdata.getCell(1, y).getContents().equals(thisApiId)){
                adm.catchdata.put(
                        DataReplace.replace(acdm, sheetCatchdata.getCell(2, y).getContents()),
                        DataReplace.replace(acdm, sheetCatchdata.getCell(3, y).getContents())
                );
            }
        }
    }

//    读取分析需求
    public void readResultAnalysis(ApiDataModel adm, ApiConfigDataModel acdm) {
        //        getCell(x,y)   第x列col , 第y行row
//        System.out.println("--debug--readApiBase--apiId: "+thisApiId);
//        Sheet sheetResultAnalysis = book.getSheet("resultAnalysis");
        int row = sheetResultAnalysis.getRows();
        for (int y = 1; y < row; y++){
            if (sheetResultAnalysis.getCell(0, y).getContents().equals("0"))
                continue;
            if (sheetResultAnalysis.getCell(1, y).getContents().equals(thisApiId)){
                TestResultAnalysisModel testResultAnalysisModel = new TestResultAnalysisModel();
                testResultAnalysisModel.expect = DataReplace.replace(acdm, sheetResultAnalysis.getCell(2, y).getContents());
                adm.testResultAnalysisModelList.add(testResultAnalysisModel);
            }
        }
    }
}
